package com.tiger.web.controller.communal;

import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.communal.domain.HydropowerSubmeter;
import com.tiger.communal.service.IHydropowerSubmeterService;
import com.tiger.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 水电分表Controller
 *
 * @author standard
 * @date 2022-10-13
 */
@Api(tags = "水电分表")
@RestController
@RequestMapping("/system/submeter")
public class HydropowerSubmeterController extends BaseController {
    @Autowired
    private IHydropowerSubmeterService hydropowerSubmeterService;

    /**
     * 查询水电分表列表
     */
    @ApiOperation(value = "查询水电分表列表")
    //@PreAuthorize("@ss.hasPermi('system:submeter:list')")
    @GetMapping("/list")
    public TableDataInfo list(HydropowerSubmeter hydropowerSubmeter) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        hydropowerSubmeter.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<HydropowerSubmeter> list = hydropowerSubmeterService.selectHydropowerSubmeterList(hydropowerSubmeter);
        return getDataTable(list);
    }


    /**
     * 获取水电分表详细信息
     */
    @ApiOperation(value = "获取水电分表列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:submeter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(hydropowerSubmeterService.selectHydropowerSubmeterById(id));
    }

    /**
     * 新增水电分表
     */
    @ApiOperation(value = "新增水电分表")
    //@PreAuthorize("@ss.hasPermi('system:submeter:add')")
//    @Log(title = "水电分表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody HydropowerSubmeter hydropowerSubmeter) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        hydropowerSubmeter.setCompanyId(loginUser.getUser().getDeptId());
        hydropowerSubmeter.setIsDeleted(0L);
        hydropowerSubmeter.setCreatedAt(DateUtils.getNowDate());
        return toAjax(hydropowerSubmeterService.insertHydropowerSubmeter(hydropowerSubmeter));
    }

    /**
     * 修改水电分表
     */
    @ApiOperation(value = "修改水电分表")
    //@PreAuthorize("@ss.hasPermi('system:submeter:edit')")
//    @Log(title = "水电分表", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody HydropowerSubmeter hydropowerSubmeter) {
        return toAjax(hydropowerSubmeterService.updateHydropowerSubmeter(hydropowerSubmeter));
    }

    /**
     * 删除水电分表
     */
    @ApiOperation(value = "删除水电分表")
    //@PreAuthorize("@ss.hasPermi('system:submeter:remove')")
//    @Log(title = "水电分表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hydropowerSubmeterService.deleteHydropowerSubmeterByIds(ids));
    }

    /**
     * 选择关联资产
     */
    @ApiOperation(value = "查看分表关联资产列表")
    @GetMapping("/getAssetList")
    public AjaxResult getAssetList(@ApiParam(value = "总表id") Long id){
        return AjaxResult.success(hydropowerSubmeterService.getAssetList(id));
    }

}
